import 'package:flutter/material.dart';
import 'elegance_base.dart';

/// Elegance控件库的主题系统
/// 
/// 这个文件定义了主题相关的类和工具，用于统一管理控件的外观样式


/// Elegance主题配置类
/// 
/// 包含了所有控件的默认样式和颜色配置（仅定义种类，不包含具体值）
class EleganceThemeData with ObservableMixin {
  /// 创建一个Elegance主题实例
  EleganceThemeData({

    // 主题色系列
    required Color primaryColor,
    required Color primaryDark,
    required Color primaryDisabled,
    required Color primaryLight,
    
    // 成功色系列
    required Color successColor,
    required Color successDark,
    required Color successDisabled,
    required Color successLight,
    
    // 错误色系列
    required Color errorColor,
    required Color errorDark,
    required Color errorDisabled,
    required Color errorLight,
    
    // 警告色系列
    required Color warningColor,
    required Color warningDark,
    required Color warningDisabled,
    required Color warningLight,
    
    // 信息色系列
    required Color infoColor,
    required Color infoDark,
    required Color infoDisabled,
    required Color infoLight,
    
    // 文本颜色系列
    required Color textPrimaryColor,
    required Color textRegularColor,
    required Color textSecondaryColor,
    required Color textPlaceholderColor,
    
    // 背景和边框颜色
    required Color backgroundColor,
    required Color surfaceColor,
    required Color borderColor,
    
    // 圆角配置
    required double borderRadius,
    required double borderRadiusSmall,
    required double borderRadiusLarge,
    required double borderRadiusXLarge,
    required double borderRadiusFull,
    
    // 边框宽度
    required double borderWidth,
    required double borderWidthThin,
    required double borderWidthThick,
    
    // 间距配置
    required double spacingXS,
    required double spacingS,
    required double spacingM,
    required double spacingL,
    required double spacingXL,
    required double spacingXXL,
    
    // 文本样式
    required TextStyle textStyle,
    required TextStyle textStyleSmall,
    required TextStyle textStyleLarge,
    required TextStyle textStyleHeading1,
    required TextStyle textStyleHeading2,
    required TextStyle textStyleHeading3,
    required TextStyle textStyleHeading4,
    required TextStyle textStyleHeading5,
    required TextStyle textStyleHeading6,
    required TextStyle buttonTextStyle,
    required TextStyle buttonTextStyleSmall,
    required TextStyle buttonTextStyleLarge,
    
    // 阴影配置
    required BoxShadow shadowSmall,
    required BoxShadow shadowMedium,
    required BoxShadow shadowLarge,
    required List<BoxShadow> shadowElevation1,
    required List<BoxShadow> shadowElevation2,
    required List<BoxShadow> shadowElevation3,
    required List<BoxShadow> shadowElevation4,
    
    // 自定义属性
    Map<String, dynamic>? customProperties,
  }) : 
    _primaryColor = primaryColor,
    _primaryDark = primaryDark,
    _primaryDisabled = primaryDisabled,
    _primaryLight = primaryLight,
    _successColor = successColor,
    _successDark = successDark,
    _successDisabled = successDisabled,
    _successLight = successLight,
    _errorColor = errorColor,
    _errorDark = errorDark,
    _errorDisabled = errorDisabled,
    _errorLight = errorLight,
    _warningColor = warningColor,
    _warningDark = warningDark,
    _warningDisabled = warningDisabled,
    _warningLight = warningLight,
    _infoColor = infoColor,
    _infoDark = infoDark,
    _infoDisabled = infoDisabled,
    _infoLight = infoLight,
    _textPrimaryColor = textPrimaryColor,
    _textRegularColor = textRegularColor,
    _textSecondaryColor = textSecondaryColor,
    _textPlaceholderColor = textPlaceholderColor,
    _backgroundColor = backgroundColor,
    _surfaceColor = surfaceColor,
    _borderColor = borderColor,
    _borderRadius = borderRadius,
    _borderRadiusSmall = borderRadiusSmall,
    _borderRadiusLarge = borderRadiusLarge,
    _borderRadiusXLarge = borderRadiusXLarge,
    _borderRadiusFull = borderRadiusFull,
    _borderWidth = borderWidth,
    _borderWidthThin = borderWidthThin,
    _borderWidthThick = borderWidthThick,
    _spacingXS = spacingXS,
    _spacingS = spacingS,
    _spacingM = spacingM,
    _spacingL = spacingL,
    _spacingXL = spacingXL,
    _spacingXXL = spacingXXL,
    _textStyle = textStyle,
    _textStyleSmall = textStyleSmall,
    _textStyleLarge = textStyleLarge,
    _textStyleHeading1 = textStyleHeading1,
    _textStyleHeading2 = textStyleHeading2,
    _textStyleHeading3 = textStyleHeading3,
    _textStyleHeading4 = textStyleHeading4,
    _textStyleHeading5 = textStyleHeading5,
    _textStyleHeading6 = textStyleHeading6,
    _buttonTextStyle = buttonTextStyle,
    _buttonTextStyleSmall = buttonTextStyleSmall,
    _buttonTextStyleLarge = buttonTextStyleLarge,
    _shadowSmall = shadowSmall,
    _shadowMedium = shadowMedium,
    _shadowLarge = shadowLarge,
    _shadowElevation1 = shadowElevation1,
    _shadowElevation2 = shadowElevation2,
    _shadowElevation3 = shadowElevation3,
    _shadowElevation4 = shadowElevation4,
    _customProperties = customProperties ?? {};
  
  // 主题色系列
  final Color _primaryColor;
  Color get primaryColor => _primaryColor;
  
  final Color _primaryDark;
  Color get primaryDark => _primaryDark;
  
  final Color _primaryDisabled;
  Color get primaryDisabled => _primaryDisabled;
  
  final Color _primaryLight;
  Color get primaryLight => _primaryLight;
  
  // 成功色系列
  final Color _successColor;
  Color get successColor => _successColor;
  
  final Color _successDark;
  Color get successDark => _successDark;
  
  final Color _successDisabled;
  Color get successDisabled => _successDisabled;
  
  final Color _successLight;
  Color get successLight => _successLight;
  
  // 错误色系列
  final Color _errorColor;
  Color get errorColor => _errorColor;
  
  final Color _errorDark;
  Color get errorDark => _errorDark;
  
  final Color _errorDisabled;
  Color get errorDisabled => _errorDisabled;
  
  final Color _errorLight;
  Color get errorLight => _errorLight;
  
  // 警告色系列
  final Color _warningColor;
  Color get warningColor => _warningColor;
  
  final Color _warningDark;
  Color get warningDark => _warningDark;
  
  final Color _warningDisabled;
  Color get warningDisabled => _warningDisabled;
  
  final Color _warningLight;
  Color get warningLight => _warningLight;
  
  // 信息色系列
  final Color _infoColor;
  Color get infoColor => _infoColor;
  
  final Color _infoDark;
  Color get infoDark => _infoDark;
  
  final Color _infoDisabled;
  Color get infoDisabled => _infoDisabled;
  
  final Color _infoLight;
  Color get infoLight => _infoLight;
  
  // 文本颜色系列
  final Color _textPrimaryColor;
  Color get textPrimaryColor => _textPrimaryColor;
  
  final Color _textRegularColor;
  Color get textRegularColor => _textRegularColor;
  
  final Color _textSecondaryColor;
  Color get textSecondaryColor => _textSecondaryColor;
  
  final Color _textPlaceholderColor;
  Color get textPlaceholderColor => _textPlaceholderColor;
  
  // 背景和边框颜色
  final Color _backgroundColor;
  Color get backgroundColor => _backgroundColor;
  
  final Color _surfaceColor;
  Color get surfaceColor => _surfaceColor;
  
  final Color _borderColor;
  Color get borderColor => _borderColor;
  
  // 圆角配置
  final double _borderRadius;
  double get borderRadius => _borderRadius;
  
  final double _borderRadiusSmall;
  double get borderRadiusSmall => _borderRadiusSmall;
  
  final double _borderRadiusLarge;
  double get borderRadiusLarge => _borderRadiusLarge;
  
  final double _borderRadiusXLarge;
  double get borderRadiusXLarge => _borderRadiusXLarge;
  
  final double _borderRadiusFull;
  double get borderRadiusFull => _borderRadiusFull;
  
  // 边框宽度
  final double _borderWidth;
  double get borderWidth => _borderWidth;
  
  final double _borderWidthThin;
  double get borderWidthThin => _borderWidthThin;
  
  final double _borderWidthThick;
  double get borderWidthThick => _borderWidthThick;
  
  // 间距配置
  final double _spacingXS;
  double get spacingXS => _spacingXS;
  
  final double _spacingS;
  double get spacingS => _spacingS;
  
  final double _spacingM;
  double get spacingM => _spacingM;
  
  final double _spacingL;
  double get spacingL => _spacingL;
  
  final double _spacingXL;
  double get spacingXL => _spacingXL;
  
  final double _spacingXXL;
  double get spacingXXL => _spacingXXL;
  
  // 文本样式
  final TextStyle _textStyle;
  TextStyle get textStyle => _textStyle;
  
  final TextStyle _textStyleSmall;
  TextStyle get textStyleSmall => _textStyleSmall;
  
  final TextStyle _textStyleLarge;
  TextStyle get textStyleLarge => _textStyleLarge;
  
  final TextStyle _textStyleHeading1;
  TextStyle get textStyleHeading1 => _textStyleHeading1;
  
  final TextStyle _textStyleHeading2;
  TextStyle get textStyleHeading2 => _textStyleHeading2;
  
  final TextStyle _textStyleHeading3;
  TextStyle get textStyleHeading3 => _textStyleHeading3;
  
  final TextStyle _textStyleHeading4;
  TextStyle get textStyleHeading4 => _textStyleHeading4;
  
  final TextStyle _textStyleHeading5;
  TextStyle get textStyleHeading5 => _textStyleHeading5;
  
  final TextStyle _textStyleHeading6;
  TextStyle get textStyleHeading6 => _textStyleHeading6;
  
  final TextStyle _buttonTextStyle;
  TextStyle get buttonTextStyle => _buttonTextStyle;
  
  final TextStyle _buttonTextStyleSmall;
  TextStyle get buttonTextStyleSmall => _buttonTextStyleSmall;
  
  final TextStyle _buttonTextStyleLarge;
  TextStyle get buttonTextStyleLarge => _buttonTextStyleLarge;
  
  // 阴影配置
  final BoxShadow _shadowSmall;
  BoxShadow get shadowSmall => _shadowSmall;
  
  final BoxShadow _shadowMedium;
  BoxShadow get shadowMedium => _shadowMedium;
  
  final BoxShadow _shadowLarge;
  BoxShadow get shadowLarge => _shadowLarge;
  
  final List<BoxShadow> _shadowElevation1;
  List<BoxShadow> get shadowElevation1 => _shadowElevation1;
  
  final List<BoxShadow> _shadowElevation2;
  List<BoxShadow> get shadowElevation2 => _shadowElevation2;
  
  final List<BoxShadow> _shadowElevation3;
  List<BoxShadow> get shadowElevation3 => _shadowElevation3;
  
  final List<BoxShadow> _shadowElevation4;
  List<BoxShadow> get shadowElevation4 => _shadowElevation4;
  
  // 自定义属性
  final Map<String, dynamic> _customProperties;
  Map<String, dynamic> get customProperties => _customProperties;
  
  
  /// 创建一个修改了指定属性的新主题实例
  /// 
  /// 其他未指定的属性将保持不变
  EleganceThemeData copyWith({
    Color? primaryColor,
    Color? primaryDark,
    Color? primaryDisabled,
    Color? primaryLight,
    Color? successColor,
    Color? successDark,
    Color? successDisabled,
    Color? successLight,
    Color? errorColor,
    Color? errorDark,
    Color? errorDisabled,
    Color? errorLight,
    Color? warningColor,
    Color? warningDark,
    Color? warningDisabled,
    Color? warningLight,
    Color? infoColor,
    Color? infoDark,
    Color? infoDisabled,
    Color? infoLight,
    Color? textPrimaryColor,
    Color? textRegularColor,
    Color? textSecondaryColor,
    Color? textPlaceholderColor,
    Color? backgroundColor,
    Color? surfaceColor,
    Color? borderColor,
    double? borderRadius,
    double? borderRadiusSmall,
    double? borderRadiusLarge,
    double? borderRadiusXLarge,
    double? borderRadiusFull,
    double? borderWidth,
    double? borderWidthThin,
    double? borderWidthThick,
    double? spacingXS,
    double? spacingS,
    double? spacingM,
    double? spacingL,
    double? spacingXL,
    double? spacingXXL,
    TextStyle? textStyle,
    TextStyle? textStyleSmall,
    TextStyle? textStyleLarge,
    TextStyle? textStyleHeading1,
    TextStyle? textStyleHeading2,
    TextStyle? textStyleHeading3,
    TextStyle? textStyleHeading4,
    TextStyle? textStyleHeading5,
    TextStyle? textStyleHeading6,
    TextStyle? buttonTextStyle,
    TextStyle? buttonTextStyleSmall,
    TextStyle? buttonTextStyleLarge,
    BoxShadow? shadowSmall,
    BoxShadow? shadowMedium,
    BoxShadow? shadowLarge,
    List<BoxShadow>? shadowElevation1,
    List<BoxShadow>? shadowElevation2,
    List<BoxShadow>? shadowElevation3,
    List<BoxShadow>? shadowElevation4,
    Map<String, dynamic>? customProperties,
  }) {
    return EleganceThemeData(
      primaryColor: primaryColor ?? _primaryColor,
      primaryDark: primaryDark ?? _primaryDark,
      primaryDisabled: primaryDisabled ?? _primaryDisabled,
      primaryLight: primaryLight ?? _primaryLight,
      successColor: successColor ?? _successColor,
      successDark: successDark ?? _successDark,
      successDisabled: successDisabled ?? _successDisabled,
      successLight: successLight ?? _successLight,
      errorColor: errorColor ?? _errorColor,
      errorDark: errorDark ?? _errorDark,
      errorDisabled: errorDisabled ?? _errorDisabled,
      errorLight: errorLight ?? _errorLight,
      warningColor: warningColor ?? _warningColor,
      warningDark: warningDark ?? _warningDark,
      warningDisabled: warningDisabled ?? _warningDisabled,
      warningLight: warningLight ?? _warningLight,
      infoColor: infoColor ?? _infoColor,
      infoDark: infoDark ?? _infoDark,
      infoDisabled: infoDisabled ?? _infoDisabled,
      infoLight: infoLight ?? _infoLight,
      textPrimaryColor: textPrimaryColor ?? _textPrimaryColor,
      textRegularColor: textRegularColor ?? _textRegularColor,
      textSecondaryColor: textSecondaryColor ?? _textSecondaryColor,
      textPlaceholderColor: textPlaceholderColor ?? _textPlaceholderColor,
      backgroundColor: backgroundColor ?? _backgroundColor,
      surfaceColor: surfaceColor ?? _surfaceColor,
      borderColor: borderColor ?? _borderColor,
      borderRadius: borderRadius ?? _borderRadius,
      borderRadiusSmall: borderRadiusSmall ?? _borderRadiusSmall,
      borderRadiusLarge: borderRadiusLarge ?? _borderRadiusLarge,
      borderRadiusXLarge: borderRadiusXLarge ?? _borderRadiusXLarge,
      borderRadiusFull: borderRadiusFull ?? _borderRadiusFull,
      borderWidth: borderWidth ?? _borderWidth,
      borderWidthThin: borderWidthThin ?? _borderWidthThin,
      borderWidthThick: borderWidthThick ?? _borderWidthThick,
      spacingXS: spacingXS ?? _spacingXS,
      spacingS: spacingS ?? _spacingS,
      spacingM: spacingM ?? _spacingM,
      spacingL: spacingL ?? _spacingL,
      spacingXL: spacingXL ?? _spacingXL,
      spacingXXL: spacingXXL ?? _spacingXXL,
      textStyle: textStyle ?? _textStyle,
      textStyleSmall: textStyleSmall ?? _textStyleSmall,
      textStyleLarge: textStyleLarge ?? _textStyleLarge,
      textStyleHeading1: textStyleHeading1 ?? _textStyleHeading1,
      textStyleHeading2: textStyleHeading2 ?? _textStyleHeading2,
      textStyleHeading3: textStyleHeading3 ?? _textStyleHeading3,
      textStyleHeading4: textStyleHeading4 ?? _textStyleHeading4,
      textStyleHeading5: textStyleHeading5 ?? _textStyleHeading5,
      textStyleHeading6: textStyleHeading6 ?? _textStyleHeading6,
      buttonTextStyle: buttonTextStyle ?? _buttonTextStyle,
      buttonTextStyleSmall: buttonTextStyleSmall ?? _buttonTextStyleSmall,
      buttonTextStyleLarge: buttonTextStyleLarge ?? _buttonTextStyleLarge,
      shadowSmall: shadowSmall ?? _shadowSmall,
      shadowMedium: shadowMedium ?? _shadowMedium,
      shadowLarge: shadowLarge ?? _shadowLarge,
      shadowElevation1: shadowElevation1 ?? _shadowElevation1,
      shadowElevation2: shadowElevation2 ?? _shadowElevation2,
      shadowElevation3: shadowElevation3 ?? _shadowElevation3,
      shadowElevation4: shadowElevation4 ?? _shadowElevation4,
      customProperties: customProperties ?? Map.from(_customProperties),
    );
  }
  
  /// 创建默认的亮色主题实例
  factory EleganceThemeData.light() {
    throw UnimplementedError('light theme is not implemented');
  }
  
  /// 创建默认的深色主题实例
  factory EleganceThemeData.dark() {
    throw UnimplementedError('dark theme is not implemented');
  }
}

/// 主题提供者
/// 
/// 使用InheritedWidget实现主题的全局共享和局部覆盖
class EleganceTheme extends InheritedWidget {
  /// 创建主题提供者
  const EleganceTheme({
    super.key,
    required this.data,
    required super.child,
  });
  
  /// 主题数据
  final EleganceThemeData data;
  
  /// 从BuildContext中获取主题实例
  static EleganceThemeData of(BuildContext context) {
    final EleganceTheme? result = context.dependOnInheritedWidgetOfExactType<EleganceTheme>();
    assert(result != null, 'No EleganceTheme found in context');
    return result!.data;
  }
  
  /// 从BuildContext中获取主题实例（不监听变化）
  static EleganceThemeData? maybeOf(BuildContext context) {
    final EleganceTheme? result = context.getElementForInheritedWidgetOfExactType<EleganceTheme>()?.widget as EleganceTheme?;
    return result?.data;
  }
  
  @override
  bool updateShouldNotify(EleganceTheme oldWidget) {
    return data != oldWidget.data;
  }
}

/// 提供默认Elegance主题的Widget
class EleganceThemeProvider extends StatelessWidget {
  /// 创建主题提供者
  const EleganceThemeProvider({
    super.key,
    this.themeData,
    required this.child,
  });
  
  /// 自定义主题数据
  final EleganceThemeData? themeData;
  
  /// 子部件
  final Widget child;
  
  @override
  Widget build(BuildContext context) {
    return EleganceTheme(
      data: themeData ?? EleganceThemeData.light(),
      child: child,
    );
  }
}

/// 主题化混入
/// 
/// 为控件提供主题访问能力
mixin ThemedMixin<T extends StatefulWidget> on State<T> {
  /// 获取当前主题
  EleganceThemeData get theme {
    final theme = EleganceTheme.maybeOf(context);
    return theme ?? EleganceThemeData.light();
  }
}

/// 扩展BuildContext以直接访问Elegance主题
extension EleganceThemeExtension on BuildContext {
  /// 获取Elegance主题
  EleganceThemeData get eleganceTheme => EleganceTheme.of(this);
}